package dao;

import java.util.ArrayList;
import java.util.List;

import model.Item;
import model.User;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

public class ItemDAO {
	@SessionTarget
	Session session;

	@TransactionTarget
	Transaction transaction;

	public void saveItem(Item item) {
		try {
			session.saveOrUpdate(item);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}
	}

	public void delete(Item item) {
		try {
			session.saveOrUpdate(item);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}
	}

	public static Integer count() {
		// TODO Auto-generated method stub
		return 6;
	}

	public static List<Item> All() {
		// TODO Auto-generated method stub
		List<Item> list = new ArrayList<Item>();
		list.add(new Item());
		return list;
	}

	public Item findById(int id) {
		session.createCriteria("from Item item where item.id=:id");
		Query q = session.createQuery("from User user where user.username = :username or user.dni = :username");
		q.setParameter("id", id);
		return (Item) q.uniqueResult();
		
		
	}
}
